package net.thelostlands.plugins.thelostfortress;

import java.util.HashSet;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockBurnEvent;
import org.bukkit.event.block.BlockPlaceEvent;

/**
 *
 * @author fabio
 */
public class ListenToBlock implements Listener {

    HashSet<Material> unbreakable, placeable;

    public ListenToBlock(HashSet<Material> breakable, HashSet<Material> placeable) {
        this.unbreakable = breakable;
        this.placeable = placeable;
    }

    @EventHandler
    public void onBlockBreak(BlockBreakEvent e) {
        Block b = e.getBlock();
        if ((b.getWorld().getName().equals(Main.mondo) && Main.area.find(b.getX(), b.getY(), b.getZ()))) {
            if (Main.permissions.has(e.getPlayer(), "thelostfortress.admin") || Main.construction || !unbreakable.contains(b.getType())) {
                if (e.getBlock().getType().equals(Material.CHEST)) {
                    if (Main.construction) {
                        Main.area.removeChest(e.getBlock().getLocation());
                        e.getPlayer().sendMessage(ChatColor.RED + "Hai rotto una cassa!");
                    } else {
                        e.setCancelled(true);
                        return;
                    }
                }

                return;
            } else {
                e.getPlayer().sendMessage(ChatColor.GREEN + "Non puoi rompere i blocchi...");
                e.setCancelled(true);

            }
        }

    }

@EventHandler
    public void onBlockPlace(BlockPlaceEvent e) {

        Block b = e.getBlock();
        if ((b.getWorld().getName().compareTo(Main.mondo) == 0 && Main.area.find(b.getX(), b.getY(), b.getZ()))) {
            if (Main.permissions.has(e.getPlayer(), "thelostfortress.admin") || Main.construction || placeable.contains(b.getType())) {
                if (e.getBlock().getType().equals(Material.CHEST)) {
                    if (Main.construction) {
                        Main.area.addChest(e.getBlock().getLocation());
                        e.getPlayer().sendMessage(ChatColor.GREEN + "Hai piazzato una chest valida!");
                    } else {
                        e.setCancelled(true);
                        return;
                    }

                }
                return;
            } else {
                e.getPlayer().sendMessage(ChatColor.GREEN + "Non puoi posizionare blocchi...");
                e.setCancelled(true);
            }
        }
    }

@EventHandler
    public void onBlockBurn(BlockBurnEvent e) {

        Block b = e.getBlock();
        if ((b.getWorld().getName().compareTo(Main.mondo) == 0 && Main.area.find(b.getX(), b.getY(), b.getZ()))) {
            e.setCancelled(true);
        }
    }
}
